package com.ylzinfo.jck.service.impl;

import com.ylzinfo.jck.common.Const;
import com.ylzinfo.jck.dao.Ac01AMapper;
import com.ylzinfo.jck.dao.Ac01LogMapper;
import com.ylzinfo.jck.dao.Ac01Mapper;
import com.ylzinfo.jck.dao.Ac02Mapper;
import com.ylzinfo.jck.domain.Ac01;
import com.ylzinfo.jck.domain.Ac01A;
import com.ylzinfo.jck.domain.Ac01Log;
import com.ylzinfo.jck.domain.Ac02;
import com.ylzinfo.jck.service.Ac01aService;
import com.ylzinfo.jck.util.CkkUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author sunqy
 * @create 2019/11/20
 */
@Service
public class Ac01aServiceImpl implements Ac01aService {

    private static Logger logger = LoggerFactory.getLogger(Ac01aServiceImpl.class);

    @Autowired(required = false)
    private Ac01AMapper ac01AMapper;

    @Autowired(required = false)
    private Ac01Mapper ac01Mapper;

    @Autowired(required = false)
    private Ac01LogMapper ac01LogMapper;

    @Autowired(required = false)
    private Ac02Mapper ac02Mapper;


    /**
     * 单个插入
     * @param ac01A
     * @return
     */
    @Override
    public int addAc01A(Ac01A ac01A) {
        ac01A.setState("0");
        return ac01AMapper.insert(ac01A);
    }

    /**
     * 批量插入
     * @param list
     * @return
     */
    @Override
    public int addAc01As(List<Ac01A> list) {
        return ac01AMapper.insertForeach(list);
    }

    /**
     * 定时任务，将各业务系统同步过来的ac01a中的数据经过处理同步到ac01中，并备份至ac01log
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void dealAc01aToAc01(Ac01A ac01A) {
        String aac002old = ac01A.getAac002old();//原aac002字段
        Ac01 ac01 = ac01Mapper.selectByAac002(ac01A.getAac002());//查询身份证号码是否存在
        BigDecimal aac001 = null;
        Ac01 ac01old = null;
        BigDecimal aac001old = null;
        Ac01Log ac01Log = new Ac01Log();

        if (ac01 == null){//新增人员基本信息
            ac01 = new Ac01();
            BeanUtils.copyProperties(ac01A, ac01);
            try {
                //调用持卡库查询接口
                String aac998 = "";
                LinkedHashMap<String, String> hashMap = CkkUtil.getPerInfoByJck(ac01);
                //若aac998为空，调用持卡库新增接口返回aac998本地保存再调用变更接口
                if (hashMap.get("aac998") == null || "".equals(hashMap.get("aac998"))){
                    aac998 = CkkUtil.addPersonInfoByJck(ac01);
                }else {
                    aac998 = hashMap.get("aac998");
                }
                //若aac998不为空，调用查询接口获得版本号等其他信息 aac998本地保存再调用变更接口
                ac01.setAac998(Long.parseLong(aac998));
                hashMap = CkkUtil.getPerInfoByJck(ac01);
                CkkUtil.updatePersonInfoByJck(ac01,hashMap);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //ac01已在mapper里面设置主键aac001，执行完下面这条，ac01里面的aac001就有值了
            ac01Mapper.insert(ac01);//直接插入ac01
        } else{//更新人员基本信息
            aac001 = ac01.getAac001();
            BeanUtils.copyProperties(ac01A, ac01);
            //上面copy后，ac01中的aac001变成各业务系统推过来的aac001，要进行更新
            ac01.setAac001(aac001);
            try {
                String aac998 = "";
                LinkedHashMap<String, String> hashMap;
                //查询本地是否有aac998 若无
                if (ac01.getAac998() == null || ac01.getAac998() == 0){
                    //调用持卡库查询接口
                    hashMap = CkkUtil.getPerInfoByJck(ac01);
                    //若aac998为空，调用持卡库新增接口返回aac998本地保存再调用变更接口
                    if (hashMap.get("aac998") == null || "".equals(hashMap.get("aac998"))){
                        aac998 = CkkUtil.addPersonInfoByJck(ac01);
                    }else {
                        aac998 = hashMap.get("aac998");
                    }
                    ac01.setAac998(Long.parseLong(aac998));
                }
                //本地存在aac998 调用查询接口获得版本号等其他信息 则调用持卡库变更接口
                hashMap = CkkUtil.getPerInfoByJck(ac01);
                CkkUtil.updatePersonInfoByJck(ac01,hashMap);
            } catch (Exception e) {
                e.printStackTrace();
            }
            ac01Mapper.updateByPrimaryKeySelective(ac01);//更新ac01
        }
        BeanUtils.copyProperties(ac01,ac01Log);
        ac01LogMapper.insert(ac01Log);//插入log表
        //如果是变更身份证
        if (!StringUtils.isEmpty(aac002old)){//原aac002非空，说明是变更aac002
            final BigDecimal aac001new = ac01.getAac001();
            ac01old = ac01Mapper.selectByAac002(aac002old);//查询旧的身份证号码对应的人员信息
            if (ac01old != null){//可能原本身份证没有同步到基础库这边
                aac001old = ac01old.getAac001();
                List<Ac02> ac02List = ac02Mapper.selectByAac001(aac001old);
                if (!CollectionUtils.isEmpty(ac02List)){
                    //将旧身份证对应的相应的险种ac02打为无效aae100='0'
//                ac02Mapper.updateAc02WhenModAac002Foreach(ac02List);//这个方法有问题，换一个方法
                    ac02Mapper.updateByAac001(aac001old);
                    //将旧身份证中对应的ac02插入到新的身份证对应的ac02中
                    ac02List.forEach(ac02 -> {
                        ac02.setAac001(aac001new);
                    });
                    ac02Mapper.insertAc02WhenModAac002Foreach(ac02List);
                }
            }
        }
        ac01A.setState(Const.DealStatusEnum.OK.getCode());//更新标志位，处理成功
        ac01A.setMsg(Const.DealStatusEnum.OK.getMsg());
    }

}
